package cn.geoportal.benoserver.controller;

import cn.geoportal.benoserver.entity.IotInfo;
import cn.geoportal.benoserver.entity.PoiInfo;
import cn.geoportal.benoserver.entity.common.PageResult;
import cn.geoportal.benoserver.entity.common.RespBean;
import cn.geoportal.benoserver.entity.vo.iot.IotInfoAdd;
import cn.geoportal.benoserver.entity.vo.iot.IotInfoGet;
import cn.geoportal.benoserver.service.IotinfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ProjectName: gmo
 * @Package: cn.geoportal.benoserver.controller
 * @ClassName: IotInfoController
 * @Author: chiangbt@geoportal.cn
 * @Description: IotInfo
 * @Date: 2021/10/1 22:46
 */
@Api(tags = "6.IOT库")
@ApiSupport(order = 401)    // 分组排序
@RestController
@RequestMapping("/api/iotinfo")
public class IotInfoController {

    @Autowired
    private IotinfoService iotinfoService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "IoT表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageNo", value = "当前页号", required = true, example = "1"),
            @ApiImplicitParam(name="pageSize", value = "批次数量", required = true, example = "15"),
            @ApiImplicitParam(name = "query",value = "名称", required = false, example = "")
    })
    @RequestMapping(value="/", method = RequestMethod.GET)
    public RespBean list(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                         @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize,
                         @RequestParam(value="query", defaultValue = "") String query){
        QueryWrapper<IotInfo> wrapper = new QueryWrapper<>();
        wrapper.like("a.name", query.toLowerCase()).or().like("a.name", query.toUpperCase())
                .or().like("a.description", query.toLowerCase()).or().like("a.description", query.toUpperCase());
        Page<IotInfo> ipage = new Page<>(pageNo, pageSize);
        try{
            IPage<IotInfo> dt = iotinfoService.queryIotInfoPage(ipage, wrapper);
            PageResult<?> pageResult = new PageResult<IotInfo>(dt.getCurrent(), dt.getSize(), dt.getTotal(), dt.getRecords());
            return RespBean.success("获取成功", pageResult);
        }catch(Exception e){
            return RespBean.error("无数据");
        }
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "添加IOT")
    @PostMapping(value = "", consumes = "application/json", produces = "application/json")
    public RespBean add(@RequestBody IotInfoAdd iotInfoAdd){
        try{
            int result = iotinfoService.add(iotInfoAdd);
            if(0 == result){
                return RespBean.error("添加的数据重复");
            }
            return RespBean.success("添加成功", result);
        }catch (Exception exp){
            System.out.println(exp.getMessage());
            return RespBean.error("添加不成功");
        }
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "获取指定id的IOT")
    @GetMapping(value = "/{id}", produces = "application/json")
    public RespBean getById(@PathVariable(value="id") Integer id){
        try{
            IotInfoGet iotinfo = iotinfoService.getIotInfoById(id);
            if(null == iotinfo){
                return RespBean.error("记录不存在");
            }
            return RespBean.success( "获取成功", iotinfo);
        }catch (Exception exp){
            return RespBean.error("获取不成功");
        }
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "更新指定id对象")
    @PatchMapping(value = "/{id}", produces = "application/json")
    public RespBean update(@PathVariable(value="id") Integer id, @RequestBody IotInfo iotinfo){
        try{
            iotinfo.setId(id);
            int result = iotinfoService.updateIotInfo(iotinfo);
            if(0 == result){
                return RespBean.error("指定更新的数据不存在");
            }
            if(-1 == result){
                return RespBean.error("指定更新的记录与已有记录的name值重复");
            }
            return RespBean.success("更新成功", result);
        }catch (Exception exp){
            return RespBean.error("更新不成功");
        }
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "删除指定id对象")
    @DeleteMapping(value = "/{id}", produces = "application/json")
    public RespBean delete(@PathVariable(value="id") Integer id){
        try{
            int result = iotinfoService.deleteIotInfo(id);
            if(0 == result){
                return RespBean.error("需要删除的数据不存在");
            }
            return RespBean.success("删除成功", result);
        }catch (Exception exp){
            return RespBean.error("删除不成功");
        }
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "查询所在点radius内的IoT")
    @ApiImplicitParams({
            @ApiImplicitParam(name="lat", value = "纬度", required = true, example = "32.033305"),
            @ApiImplicitParam(name="lng", value = "经度", required = true, example = "118.850675"),
            @ApiImplicitParam(name = "radius",value = "范围距离", required = false, example = "1000")
    })
    @RequestMapping(value = "/distance", method = RequestMethod.GET)
    public RespBean getAllIotInfoList(@RequestParam(value = "lat", defaultValue = "32.033305") Float lat,
                                       @RequestParam(value = "lng", defaultValue = "118.850675") Float lng,
                                       @RequestParam(value="radius", defaultValue = "1000") Float radius){
        try{
            List<IotInfo> shopInfos = iotinfoService.findWithDistance(lat, lng, radius);

            return RespBean.success("获取成功", shopInfos);
        }catch (Exception e){
            return RespBean.error("无数据");
        }
    }
}
